<template>
	<view class="content">
		<!-- <image class="logo" src="/static/logo.png"></image> -->
		<!-- <image class="outer__image" src="http://nas.zcloudtechs.cn:9009/3098-usercenter/user/2021-03-01/jpg/768bdebc-587c-4c5e-85e0-d3b9cedd53af.jpg"></image>
		<view class="text-area">
			<text class="title">{{title}}wsnm</text>
		</view> -->
	</view>
</template>

<script>
	import { login, queryPlaceAppid } from '@api/login/index'
	import { serializeToUrl, isEmpty, getUrlParams } from '@common/utils.js'
	import { clearStorage, setDirect, setXTenantHeader, getMpFlag, setMpFlag } from '@common/auth.js';
	import { mapActions, mapGetters } from 'vuex'
	export default {
		data() {
			return {
				title: 'map',
				token: ''
			}
		},
		onLoad(option) {
			// if (!isEmpty(this.token)) return;
			this.getToken();
			// this.loginTest();
		},
		computed: {
			...mapGetters('app', ['menuActive'])
		},
		methods: {
			...mapActions('user', ['userByWxCode']),
			getCode() {
				queryPlaceAppid('3098').then(({ data, success, msg }) => {
					if (!success) return console.log(9999);
					// this.getToken(data);
				})
			},
			getToken(data) {
				const { code } = getUrlParams();
				if (!isEmpty(code)) return this.loadUserInfo();
				const redirect = {
					param: {
					  appId: 'wxd0b90fdf369aeca3',
					  redirect_uri: encodeURIComponent(location.href),
					  response_type: 'code',
					  scope: 'snsapi_userinfo',
					  state: '0'
					},
					url: 'https://open.weixin.qq.com/connect/oauth2/authorize',
					hash: '#wechat_redirect'
				};
				const redirectParam = serializeToUrl(redirect.param, false);
				const redirectUrl = `${redirect.url}?${redirectParam}${redirect.hash}`;
				console.log(redirectUrl)
				return location.replace(redirectUrl);
			},
			loadUserInfo() {
				var { code, placeId, direct, lockCode } = getUrlParams();
				/* 获取完code，存储数据 */
				if (!isEmpty(placeId)) setXTenantHeader(placeId);
				// 判断是否有直连
				if (direct) setDirect(direct);
								
				// 用于获取code进行本地调试
				if (lockCode) return console.log('[log:code]', code);
								
				// 调用后台，code换取用户信息
				this.userByWxCode({ code, placeId }).then(({ success, data }) => {
				  // this.$nextTick(() => {
				  //   if (!success) this.$router.replace('/login');
				  // });
				}).catch((error) => {
					uni.navigateTo({
						url: 'pages/index/login'
					})
				});
				        
			},
			loginTest() {
				  var data = {
				    placeId: '3098',
				    password: 'test123456',
				    username: 'cy',
					roleCode: 'supermanager'
				  };
				  login(data).then(({ success, msg, data }) => {
				    if (!success)
				      return uni.showToast({
				        icon: 'success',
				        title: msg,
				        mask: true
				      });
				    uni.setStorageSync('token', data.token);
				    uni.setStorageSync('placeId', '3098');
				  });
			}
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	.outer {
		width:100px;
		height:100px;
		border-radius: 50%;
		overflow: hidden;
		&__image {
			width: 100px;
		}
	}
</style>
